<template>
    <div class='approval'>
        <el-card class="box-card">
            <div class='title'>
            {{$t('achievements.title1')}}
            </div>
            <div class='menu-search'> 
                <div class='menu-title'>
                    <div class='searchItem searchItem-date'>
                      <span>{{$t('keyword.updateTime')}}</span>
                      <el-date-picker
                      size='small'
                      class="dates"
                      v-model="listQuery.beginDate"
                      type="date"
                      value-format="yyyy-MM-dd"
                      :placeholder="$t('rule.beginDate')">
                      </el-date-picker>
                      <b>{{$t('approval.go')}}</b>
                      <el-date-picker
                      size='small'
                      class="dates"
                      v-model="listQuery.endDate"
                      type="date"
                      value-format="yyyy-MM-dd"
                      :placeholder="$t('rule.endDate')">
                      </el-date-picker>
                  </div>
                    <div class='menu-title-btn' style="margin-left: 10px;">
                        <button type='button' @click='search'>
                            {{$t('btn.query')}}
                        </button>
                        <button type='button' @click='reset'>
                            {{$t('btn.reset')}}
                        </button>
                    </div>
                </div>
            </div>
        </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
            <div class='title'>
                {{$t('achievements.title2')}}
            </div>
            <div class='menu-list'>
                <button type='button' @click="handleCreate">{{$t('btn.NewAdd')}}</button>
            </div>
            <el-table
                v-loading="listLoading"
                ref='multipleTable'
                :data='tableData'
                tooltip-effect='dark'
                style='width: 100%'
                border
                stripe
                size='mini'>
                <el-table-column
                header-align='center'
                align='center'
                prop='keyword'
                :label="$t('keyword.keyWords')">
                </el-table-column>
                <el-table-column
                header-align='center'
                align='center'
                prop="status"
                :label="$t('grouping.status')">
                <template slot-scope="scope">
                    <p class="able" v-if="scope.row.status === 'discontinued'">
                        <span @click="start(scope.row)">{{$t('grouping.enable')}}</span>
                        <b style="display: inline-block;margin: 0 3px;color: #afb0b2;">|</b>
                        <span style="color: #ff5e43;">{{$t('grouping.blockup')}}</span>
                    </p>
                    <p class="able" v-else-if="scope.row.status === 'release'">
                        <span style="color: #ff5e43;">{{$t('grouping.enable')}}</span>
                        <b style="display: inline-block;margin: 0 3px;color: #afb0b2;">|</b>
                        <span @click="stop(scope.row)">{{$t('grouping.blockup')}}</span>
                    </p>
                    <p v-else></p>
                </template>
                </el-table-column>
                <el-table-column
                header-align='center'
                align='center'
                prop='updateTime'
                :label="$t('keyword.updateTime')">
                </el-table-column>
                <el-table-column
                header-align='center'
                align='center'
                prop='newUpdate'
                :label="$t('keyword.newUpdate')"
                show-overflow-tooltip>
                </el-table-column>
                <el-table-column 
                align="center" 
                :label="$t('user.operation')" 
                width="150" fixed="right" > 
                    <template slot-scope="scope">
                        <el-button size="small" type="success" @click="handleUpdate(scope.row)">{{$t('btn.edit')}}
                        </el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.row)">{{$t('btn.delete')}}
                        </el-button>
                    </template> 
            </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="listQuery.page"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="listQuery.limit"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
                </el-pagination>
            </div>
        </el-card>
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :before-close = "closeDialog">
            <el-form :model="form" :rules="rules" ref="form" label-width="100px">
                <el-form-item :label="$t('keyword.keyWords')" prop="keywords">
                    <el-input size='small' :placeholder="$t('keyword.ruleKeyword')" v-model="form.keywords" style="width: 92%;"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel('form')">{{$t('btn.cancel')}}</el-button>
                <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">{{$t('btn.confirm')}}</el-button>
                <el-button v-else type="primary" @click="update('form')">{{$t('btn.confirm')}}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { page, addCashLoan, delCashLoan, putCashLoan, editStatus } from 'api/synManager/creditSupermarket/cashLoan/index'
export default {
  data() {
    return {
      form: {
        productName: '',
        sort: undefined,
        logoUrl: '',
        score: '',
        isboutique: undefined,
        borrowingRangeStart: undefined,
        borrowingRangeEnd: undefined,
        keywords: '',
        introduction: '',
        downloadUrl: '',
        md5: '',
        newsrc: ''
      },
      rules: {
        keywords: [
          { required: true, message: this.$t('creditSupermarket.ruleSlogan'), trigger: 'blur' }
        ]
      },
      textMap: {
        update: this.$t('btn.edit'),
        create: this.$t('btn.NewAdd')
      },
      dialogStatus: '',
      dialogFormVisible: false,   
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        beginDate: "",
        endDate: ""
      },
      tableData: [],
      listId: null
    };
  },
  created() {
    if (this.gamappPage.has(this.$route.path)) {
      this.listQuery = this.gamappPage.get(this.$route.path)
    }
    this.getList();
  },
  computed: {
    ...mapGetters(["elements", "gamappPage"])
  },
  methods: {
    // 启用
    start(row) {
      let obj1 = {};
      obj1.status = 'release';
      editStatus(row.id, obj1)
      .then(response => {
        if (response.status === 200) {
          this.getList()
          this.$message({
            showClose: true,
            message: this.$t('grouping.msg1'),
            type: 'success'
          });
        } else {
          this.$message({
            showClose: true,
            message: response.message,
            type: 'warning'
          });
        }
      })
    },
    // 停用
    stop(row) {
      let obj2 = {};
      obj2.status = 'discontinued';
      editStatus(row.id, obj2)
      .then(response => {
        if (response.status === 200) {
          this.getList()
          this.$message({
            showClose: true,
            message: this.$t('grouping.msg2'),
            type: 'success'
          });
        } else {
          this.$message({
            showClose: true,
            message: response.message,
            type: 'warning'
          });
        }
      })
    },
    resetTemp() {
      this.form = {
        keywords: ''
      };
    },
    closeDialog(done) {
      done();
      this.$refs["form"].resetFields();
      this.getList()
    },
    // 点击添加按钮
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    // 点击编辑按钮
    handleUpdate(row) {
      this.dialogStatus = 'update';
      this.listId = row.id;
      this.form = row;
      this.dialogFormVisible = true;
    },
    // 点击删除按钮
    handleDelete(row) {
      this.$confirm(this.$t('user.cue1'), this.$t('user.cue2'), {
        confirmButtonText: this.$t('btn.confirm'),
        cancelButtonText: this.$t('btn.cancel'),
        type: 'warning'
      })
        .then(() => {
          delCashLoan(row.id)
          .then(() => {
            this.$notify({
              title: this.$t('user.success'),
              message: this.$t('user.delsuccess'),
              type: 'success',
              duration: 2000
            });
            const index = this.tableData.indexOf(row);
            this.tableData.splice(index, 1);
          });
        })
        .catch(() => {
          this.$message({
            showClose: true,
            type: "info",
            message: this.$t('orderQuery.cue2')
          });
        });
    },
    // 添加保存按钮
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm(this.$t('loanDetail.cue1'), {
            confirmButtonText: this.$t('btn.confirm'),
            cancelButtonText: this.$t('btn.cancel'),
            type: "warning",
            center: true
          })
          .then(() => {
            addCashLoan(this.form)
              .then((response) => {
                if (response.status === 200) {
                  this.dialogFormVisible = false;
                  this.getList();
                  this.$notify({
                    title: this.$t('user.success'),
                    message: this.$t('user.crtsuccess'),
                    type: 'success',
                    duration: 2000
                  });
                } else {
                  this.$message({
                    showClose: true,
                    type: "warning",
                    message: response.message
                  });
                }
              });
          })
          .catch(() => {
            this.$message({
              showClose: true,
              type: "info",
              message: this.$t('loanDetail.cue3')
            });
          });
        } else {
          return false;
        }
      });
    },
    // 编辑保存按钮
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.form.id = this.listId;
          putCashLoan(this.form)
          .then(res => {
            if (res.status === 200) {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: this.$t('user.success'),
                message: this.$t('user.crtsuccess'),
                type: 'success',
                duration: 2000
              });
            } else {
              this.$message({
                showClose: true,
                type: "warning",
                message: response.message
              });
            }
          })
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch('saveGamappPage', { path: this.$route.path, listQuery: this.listQuery });
      page(this.listQuery)
        .then(response => {
          if (response.status === 200) {
            this.listLoading = false;
            this.tableData = response.data.records;
            this.total = Number(response.data.total);
          } else {
            this.$message({
              showClose: true,
              type: "warning",
              message: response.message
            });
          }
        })
    },
    // 查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.beginDate = '';
      this.listQuery.endDate = '';
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList()
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.href {
  border-bottom: 1px solid #000;
  cursor: pointer; 
  &:hover {
    color: #20a0ff;
    border-bottom: 1px solid #20a0ff;
  }
}

.able {
    display: inline-block;
    span {
        border-bottom: 1px solid #afb0b2;
        padding-bottom: 2px;
        color: #afb0b2;
        cursor: pointer;
    }
}
</style>
